<?php
namespace data\site;
use \data\BaseData;
/**
 * 商户管理员数据管理
 */
class AdminData extends BaseData{
    
    
    /**
     * 读取一个管理员信息
     */
    public function find($param){
        $check = ['a.adminid','a.account'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $fields = 'a.*,r.type,r.role,r.id as roleid';
        $info  = M('site_admin')->alias('a')->join('elite_sys_power_role r','a.roleid = r.id')->field( $fields)
        ->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一个管理员信息
     */
    public function add($data){        
        $check = [
            'msiteid'    => ['check'=>'must','msg'=> '站点主表ID不能为空','reg'=>'int'],
            'status'    => ['def'=>1],        
            'usercode'  => '',
            'account'   => ['check'=>'must','msg'=>'账号不能为空','reg'=>'account'],
            'password'  => ['check'=>'must','msg'=>'密码不能为空'],
            'passsalt'  => ['check'=>'must'],
            'truename'  => '',
            'gender'    => ['reg'=>[1,2,3]],
            'mobile'    => ['reg'=>'mobile'],
            'roleid'    => ['check'=>'must','msg'=> '角色不能为空','reg'=>'int'],
            'addtime'   => ['val'=> SYS_TIME],
           // 'edittime'  => ['val'=> SYS_TIME],  
            'email'     => ['reg'=> 'email']
        ];
        $save = $this->verifyData($check,$data);
        return M('site_admin')->insertGetId($save);
    }
    
    /**
     * 修改一个管理员信息
     */
    public function edit($adminid, $data){
        $check = [
            'status'    => ['reg'=>[1,2]],
            'account'   => ['reg'=>'account'],
            'password'  => ['reg'=>'must'],
            'passsalt'  => ['reg'=>'must'],
            'truename'  => ['reg'=>'must'],
            'mobile'    => ['reg'=>'mobile'],
            'roleid'    => ['reg'=>'int'],    
            'gender'    => ['reg'=>[1,2,3]],       
            'edittime'  => ['val'=> SYS_TIME],  
            'email'     => ['reg'=> 'email']
        ]; 
             
        $save = $this->verifyData($check,$data);
        if( !$save ) return false;
        $where = ['adminid'=>$adminid];
        $result = M('site_admin')->where($where)->update($save);
        if($result === false ) dataE('修改账户信息失败');
        return $result;
    }
    
}